package com.tedu.movie.service;

import com.tedu.movie.mapper.ItemMapper;

import com.tedu.movie.mapper.TorderMapper;
import com.tedu.movie.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class TorderServiceImpl implements TorderService{
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    TorderMapper torderMapper;

    @Override
    public boolean updateTorder(Torder torder) {
        int updateRow= torderMapper.updateByPrimaryKeySelective(torder);
        if(updateRow>=1){
            return  true;
        }else{
            return false;
        }
    }

    @Override
    public boolean deleteTorder(Integer id) {
        int deleteRow =torderMapper.deleteByPrimaryKey(id);
        if (deleteRow>=1){
            return true;
        }
        return false;
    }

    @Override
    public List<TorderItemVO> selectAll() {
        ArrayList<TorderItemVO> volist=new ArrayList<>();
        List<Torder> list = torderMapper.selectByExample(null);
        for(Torder torder:list){
            Item item=itemMapper.selectByPrimaryKey(torder.getItemId());
            TorderItemVO torderItemVO = new TorderItemVO();
            torderItemVO.setTotal(torder.getTotal());
            torderItemVO.setTicket(torder.getTicket());
            torderItemVO.setId(torder.getId());

            torderItemVO.setCategoryId(item.getCategoryId());
            torderItemVO.setName(item.getName());
            torderItemVO.setSeat(item.getSeat());
            torderItemVO.setImage(item.getImage());
           torderItemVO.setPrice(item.getPrice());
            torderItemVO.setItemDesc(item.getItemDesc());

            volist.add(torderItemVO);
        }
        return volist;
    }

    @Override
    public boolean insertTorder(Torder torder) {
        int insertRow =torderMapper.insertSelective(torder);
        if(insertRow>=1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Torder selectById(Integer id) {
        Torder torder = torderMapper.selectByPrimaryKey(id);
        return torder;
    }

    @Override
    public List<Torder> selectByItemId(Integer ItemId) {
        //select * from item where category_id=1
        TorderExample torderExample = new TorderExample();
        TorderExample.Criteria criteria = torderExample.or();
        criteria.andItemIdEqualTo(ItemId);
        List<Torder> list = torderMapper.selectByExample(torderExample);
        return list;
    }

    @Override
    public boolean buy(Torder torder) {
        boolean isSuccess = false;

        //OrderExample orderExample = new OrderExample();
        // OrderExample.Criteria criteria = orderExample.or();

        //criteria.andTicketEqualTo(order.getTicket());
        //List<Order> list = orderMapper.selectByExample(orderExample);
        // if (list.size() == 0) {
        int insertRow =torderMapper.insertSelective(torder);
        if (insertRow >= 1) {
            isSuccess = true;
        }

        //}


        return isSuccess;
    }
}
